package com.hmdp.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * The `RedissionConfig` class is a Spring configuration class responsible for creating and configuring a `RedissonClient` bean.
 * This bean is used to interact with a Redis server, particularly for distributed locking mechanisms.
 *
 * The class is annotated with `@Configuration`, indicating that it provides bean definitions to the Spring application context.
 * It also uses the `@Slf4j` annotation to enable logging capabilities via the SLF4J logging facade.
 *
 * The primary method in this class is `redissonClient()`, which is annotated with `@Bean`. This method creates and configures
 * a `RedissonClient` instance, which is a client for the Redisson library. The client is configured to connect to a single
 * Redis server instance using the provided address and password.
 *
 * The Redis server address is set to `redis://192.168.208.128:6379`, and the password is set to `ningzaichun`. These values
 * are hardcoded in the configuration but can be externalized to properties or environment variables for better flexibility.
 *
 * The `RedissonClient` bean created by this method can be injected into other components in the application, enabling them
 * to interact with the Redis server for distributed locking or other Redis-based operations.
 *
 * Example usage:
 * - The `RedissonClient` bean can be injected into services or components that require distributed locking or Redis operations.
 * - The client can be used to acquire and release locks, manage Redis data structures, or perform other Redis-related tasks.
 *
 * Note: This configuration assumes a single Redis server instance. For more complex setups (e.g., Redis cluster or sentinel),
 * the configuration would need to be adjusted accordingly.
 */
@Configuration
@Slf4j
public class RedissionConfig {
    /**
     * 配置redisson客户端,分布式锁使用
     *
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {
        log.info("Creating RedissonClient bean bean");
        Config config = new Config();
        //因为只有一个redis实例，所以这里只需要设置一个server
        config.useSingleServer().setAddress("redis://192.168.208.128:6379").setPassword("ningzaichun");
        return Redisson.create(config);
    }
}
